﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace System.Web.Mvc
{
    public static class MvcHelper
    {
        public static string GetControllerName<TController>() where TController : Controller
        {
            string controllerName = typeof(TController).Name;
            if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException("Controller name must end in 'Controller'.", "action");
            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
            if (controllerName.Length == 0)
                throw new ArgumentException("Cannot route to class named 'Controller'.", "action");
            return controllerName;
        }

        public static string GetControllerName(Type tController)
        {
            string controllerName = tController.Name;
            if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException("Controller name must end in 'Controller'.", "action");
            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
            if (controllerName.Length == 0)
                throw new ArgumentException("Cannot route to class named 'Controller'.", "action");
            return controllerName;
        }
    }
}